/*
 * Copyright 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import org.starsource.forum.client.domain.CategoryObject;
import org.starsource.forum.client.domain.SessionObject;
import org.starsource.forum.client.handler.SessionCreateHandler;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import org.starsource.forum.client.widget.RichTextToolbar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple example of an 'about' dialog box.
 */
public class SessionCreateDialog extends DialogBox {

	private final ForumServiceAsync forumService = GWT.create(ForumService.class);

	interface Binder extends UiBinder<Widget, SessionCreateDialog> { }
  private static final Binder binder = GWT.create(Binder.class);
  interface RichTextBoxStyle extends CssResource {
	    String richTextArea();
	  }

  private SessionCreateHandler sessionCreateHandler;

  public void setSessionCreateHandler(SessionCreateHandler sessionCreateHandler) {
	  this.sessionCreateHandler = sessionCreateHandler;
  }

  @UiField TextBox title;
  @UiField FlexTable richTextBox;
  @UiField Button submitButton;
  @UiField Button closeButton;
  @UiField RichTextBoxStyle richTextBoxStyle;

  final RichTextArea richTextArea = new RichTextArea();

  final RichTextToolbar richTextToolbar = new RichTextToolbar();

  private CategoryObject categoryObject;

  public void setCategoryObject(CategoryObject categoryObject) {
	  this.categoryObject = categoryObject;
  }

  public SessionCreateDialog() {
    // Use this opportunity to set the dialog's caption.
    setText("发表新帖");
    setWidget(binder.createAndBindUi(this));

    setAnimationEnabled(true);
    setGlassEnabled(true);

    richTextToolbar.setRichTextArea(richTextArea);

    title.addKeyUpHandler(new KeyUpHandler() {

		@Override
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				richTextArea.setFocus(true);
			}
		}

    });

    richTextArea.addStyleName(richTextBoxStyle.richTextArea());

    richTextBox.setWidget(0, 0, richTextToolbar);
    richTextBox.setWidget(1, 0, richTextArea);
  }

  @UiHandler("submitButton")
  void onSubmitClicked(ClickEvent event) {
	  submitButton.setEnabled(false);
	  forumService.createSession(title.getText(), richTextArea.getHTML(), categoryObject.getId(), new AsyncCallback<SessionObject>(){

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub

			submitButton.setEnabled(true);
		}

		@Override
		public void onSuccess(SessionObject result) {
			sessionCreateHandler.onSessionCreate(result);
			  title.setText("");
			  richTextArea.setText("");

			submitButton.setEnabled(true);

			hide();

		}

	  });
  }

  @UiHandler("closeButton")
  void onCloseClicked(ClickEvent event) {
    hide();
  }

  public void focus() {
	  title.setFocus(true);
  }
}
